﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AutoCreateCodePlus
{
    public class ConntectData
    {
        private static readonly object ContectDatalock=new object();

        private static ConntectData _instance;

        public static ConntectData Instance
        {
            get
            {
                if (_instance!=null)
                {
                    return _instance;
                }
                else
                {
                    lock (ContectDatalock)
                    {
                        return _instance ?? (_instance = new ConntectData());
                    }
                }
            }
        }

        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        /// <param name="serverName">服务器名字</param>
        /// <param name="dataBaseName">数据库名称</param>
        /// <param name="userName">登录名</param>
        /// <param name="passWord">登录密码</param>
        /// <returns>一个数据库连接字符串</returns>
        private string GetConntectString(string serverName,string dataBaseName,string userName,string passWord)
        {
            StringBuilder conBuilder=new StringBuilder();
            conBuilder.Append("Server=" + serverName + ";");
            conBuilder.Append("database=" + dataBaseName + ";");
            conBuilder.Append("uid=" + userName + ";");
            conBuilder.Append("pwd=" + passWord + ";");
            return conBuilder.ToString();
        }

        /// <summary>
        /// 测试是否可以连接连接数据库
        /// </summary>
        /// <returns></returns>
        public bool ContectData(string contectString)
        {
            bool isContect = false;
         //   string contectString = GetConntectString(serverName, dataBaseName, userName, passWord);
            using (SqlConnection connection=new SqlConnection(contectString))
            {
                try
                {
                    connection.Open();
                    isContect = true;
                }
                catch (Exception e)
                {
                   
                }
            }
            return isContect;
        }

        /// <summary>
        /// 获取所有表名
        /// </summary>
        /// <param name="contectString"></param>
        /// <returns></returns>
        public DataTable GeTable(string contectString)
        {
            DataSet ds = new DataSet();
            using (SqlConnection con=new SqlConnection(contectString))
            {
                try
                {
                    con.Open();
                    string strSql = "select name from sysobjects where xtype='U' or xtype = 'V' order by name";
                    SqlDataAdapter dr=new SqlDataAdapter(strSql,con);
                    dr.Fill(ds, "TableName");
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return ds.Tables[0];
        }

        public DataTable GetTable(string contectString, string strSql)
        {
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection(contectString))
            {
                try
                {
                    con.Open();
                    SqlDataAdapter dr = new SqlDataAdapter(strSql,con);
                    dr.Fill(ds, "Table");
                }
                catch (Exception)
                {
                    throw;
                }
            }
            //select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_name
            return ds.Tables[0];
        }

        public DataTable GetDatabaseDataTable(string contectString)
        {
            string strSql = "Select dbid,name from master..sysdatabases where name not in('master','model','msdb','tempdb','northwind','pubs')";
            return this.GetTable(contectString, strSql);
        }
    }
}
